package com.draven.serviceImpl;

import com.draven.entity.User;
import com.draven.mapper.UserMapper;
import com.draven.service.UserService;
import com.draven.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

/**
 * Created by Draven on 2017/4/9.
 * E-mail:draven1122@163.com
 */
@Service(UserService.SERVICE_NAME)
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public User getUser(String id) throws Exception {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    public int addUser(User user) throws Exception {
        int i = 0;
        i = userMapper.insert(user);
        if(i == 0){
            throw new SQLException("新增用户失败");
        }
        return i;
    }

    public int addUserSelective(User user) throws Exception {
        int i = 0;
        i = userMapper.insertSelective(user);
        if(i == 0){
            throw new SQLException("新增用户失败");
        }
        return i;
    }

    public int removeUser(String user) throws Exception {
        int i = 0;
        i = userMapper.deleteByPrimaryKey(user);
        if(i == 0){
            throw new SQLException("删除用户失败,可能不存在该用户");
        }
        return i;
    }

    public int modifyUser(User user) throws Exception {
        int i = 0;
        i = userMapper.updateByPrimaryKey(user);
        if(i == 0){
            throw new SQLException("更新用户失败，可能不存在该用户");
        }
        return i;
    }

    public int modifyUserSelective(User user) throws Exception {
        int i = 0;
        i = userMapper.updateByPrimaryKeySelective(user);
        if(i == 0){
            throw new SQLException("更新用户失败，可能不存在该用户");
        }
        return i;
    }

    public User login(String username, String password) throws Exception {
        User user = userMapper.getUserByUsernameAndPassword(username, password);
        return user;
    }
}
